/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pbb_template_updater;

import java.io.*;
import java.util.Hashtable;

/**
 *
 * @author jhuss
 */
public class PbbGoTermCollector {

    String gtName;

    public PbbGoTermCollector(String gtFileName) {

        gtName = gtFileName;

    }

    /**
     * Parses the go terms file (or potentially reads in a pre-parsed file) and returns a
     * hashtable of GO ids and values.  Takes one argument, a boolean specifying whether 
     * or not to automatically parse the original GOterm file - I.e. even if the cache file exists,
     * parse the old file anyway.
     * 
     * If there are any problems parsing the file, we return an empty hashtable (but also spit out warnings.)
     * 
     * @author jhuss
     */
    public Hashtable<String, String> retrieveGoTerms(boolean parseOriginal) {
        //we have two options - either we get the go terms from a new go file, or we get the terms
        //from a previously parsed file. First we check to see if the command line option has been set
        //to parse from a new file...

        File fileGoTerm = new File(gtName);
        File fileGoTermCache = new File(gtName + ".pbb");

        Hashtable<String, String> data = null;
        try {
            if (!parseOriginal) {
                if (fileGoTermCache.canRead()) {
                    System.out.println("Reading Pre-Parsed GO-Terms File: " + fileGoTermCache.getPath() +"\n");
                    //String input;
                    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileGoTermCache)));
                    String line = "";
                    data = new Hashtable<String, String>();
                    //glblBotLog.addEntry("SYSTEM", "GO_TERMS", "Using Pre-Parsed go terms file:" + pbbIn.getPath(), PBBLogging.logs.SY);
                    
                    //The file keys and values are separated by a single ';'
                    while ((line = br.readLine()) != null) {
                        int index = line.indexOf(";");
                        if (index != -1){
                        //System.out.println("Go values: " + line.substring(0,index) + " = " + line.substring(index+1));
                            data.put(line.substring(0, index), line.substring(index + 1));
                        }

                    }

                    System.out.println("Done.");
                    return data;

                }

            }
        } catch (Exception e) {
            //we assume some kind of read error, we will just have to parse the old file again.
            System.out.println("!!!WARNING!!! -- Pre-Parsed GO-Terms read Failed: " + e.getMessage() +"\n");
        }
        
        System.out.println("Reading GO-Terms File: " + fileGoTerm.getPath() +"\n");
        try {
            data = PbbXML.xmlGoParse(gtName);
            System.out.println("Done.");
        }catch (Exception e) {
            System.out.println("!!!ERROR!!! -- Cannot parse GO-Terms file: " + e.getMessage() +"\n");
            System.out.println("!!!WARNING!!! -- No GO Terms have been parsed.");
            data = new Hashtable<String, String>();
        }

        return data;
    }
}
